package pl.icn.hurtownia.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pl.icn.hurtownia.encje.Adres;
import pl.icn.hurtownia.utils.HibernateUtil;

/**
 * DAO dla adresów 
 */
public class AdresDAO {

	public static Long nowyAdres(String nrDomu, String nrLokalu, String ulica, String kod, String miasto) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		Long adresID = null;
		
		try {
			tx = session.beginTransaction();
			Adres adres = new Adres(nrDomu, nrLokalu, ulica, kod, miasto, null);
			adresID = (Long) session.save(adres); 
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace(); 
		} finally {
			session.close(); 
		}
		
		return adresID;
	}
	
	public static Adres daneAdresu(Long adresID) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction tx = null;
	    Adres adres = null;
	    
	    try {
	    	tx = session.beginTransaction();
	        adres = (Adres) session.get(Adres.class, adresID); 
	        tx.commit();
	    } catch (HibernateException e) {
	    	if (tx != null) {
	    		tx.rollback();
	        }
	        e.printStackTrace(); 
	    } finally {
	    	session.close(); 
	    }
	    
	    return adres;
	}
	
	public static void edycjaAdresu(Long adresID, String nrDomu, String nrLokalu, String ulica, String kod, String miasto) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction tx = null;
	    
	    try {
	    	tx = session.beginTransaction();
	    	Adres adres = (Adres) session.get(Adres.class, adresID);
	    	if (nrDomu != null) {
	    		adres.setNrDomu(nrDomu);
	    	}
	    	if (nrLokalu != null) {
	    		adres.setNrLokalu(nrLokalu);
		    }
	    	if (ulica != null) {
	    		adres.setUlica(ulica);
	    	}
	    	if (kod != null) {
	    		adres.setKod(kod);
	    	}
	    	if (miasto != null) {
	    		adres.setMiasto(miasto);
	    	}
	    	session.update(adres); 
	    	tx.commit();
    	} catch (HibernateException e) {
    		if (tx != null) {
    			tx.rollback();
    		}
    		e.printStackTrace(); 
    	} finally {
    		session.close(); 
    	}
	}
	
	public static void edycjaAdresu(Adres adres) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction tx = null;
	    
	    try {
	    	tx = session.beginTransaction();
	    	session.merge(adres); 
	    	tx.commit();
    	} catch (HibernateException e) {
    		if (tx != null) {
    			tx.rollback();
    		}
    		e.printStackTrace(); 
    	} finally {
    		session.close(); 
    	}
	}
	
	public static void usuniecieAdresu(Long adresID){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			Adres adres = (Adres) session.get(Adres.class, adresID); 
			session.delete(adres); 
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace(); 
		} finally {
			session.close(); 
		}
	}
	
}
